package com.appwx.sdk.http;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import com.appwx.sdk.model.AppParameters;
import com.appwx.sdk.model.OpenApiModel;
import com.appwx.sdk.model.Token;
import com.appwx.sdk.view.WeiboException;


public class AccessTokenHeader extends HttpHeaderFactory{
	@Override
	public AppParameters generateSignatureList(AppParameters bundle) {
		if (bundle == null || (bundle.size() == 0)) {
			return null;
		}
		AppParameters pp = new AppParameters();
		String key = "oauth_consumer_key";
		pp.add(key, bundle.getValue(key));
		key = "oauth_nonce";
		pp.add(key, bundle.getValue(key));
		key = "oauth_signature_method";
		pp.add(key, bundle.getValue(key));
		key = "oauth_timestamp";
		pp.add(key, bundle.getValue(key));
		key = "oauth_token";
		pp.add(key, bundle.getValue(key));
		key = "oauth_verifier";
		pp.add(key, bundle.getValue(key));
		key = "oauth_version";
		pp.add(key, bundle.getValue(key));
		key = "source";
		pp.add(key, bundle.getValue(key));
		return pp;
	}

	@Override
	public String generateSignature(String data, Token token)
			throws WeiboException {
		byte[] byteHMAC = null;
		try {
			Mac mac = Mac.getInstance(HttpHeaderFactory.CONST_HMAC_SHA1);
			SecretKeySpec spec = null;
			if (null == token.getSecretKeySpec()) {
				String oauthSignature = encode(OpenApiModel.getAPP_SECRET()) + "&"
						+ encode(token.getSecret());
				spec = new SecretKeySpec(oauthSignature.getBytes(),
						HttpHeaderFactory.CONST_HMAC_SHA1);
				token.setSecretKeySpec(spec);
			}
			spec = token.getSecretKeySpec();
			mac.init(spec);
			byteHMAC = mac.doFinal(data.getBytes());
		} catch (InvalidKeyException e) {
			throw new WeiboException(e);
		} catch (NoSuchAlgorithmException e) {
			throw new WeiboException(e);
		}
		return String.valueOf(Utility.base64Encode(byteHMAC));
	}

	@Override
	public void addAdditionalParams(AppParameters des, AppParameters src) {
		// TODO Auto-generated method stub

	}
}
